<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="角色名称" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="orderNum">
				<el-input-number v-model="form.orderNum" :min="1"></el-input-number>
			</el-form-item>
			<!--			
			<el-form-item label="成员数" prop="memberCount">
				<el-input v-model="form.memberCount" clearable type="input"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updateTime">
				<el-input v-model="form.updateTime" type="input"></el-input>				
			</el-form-item>
			<el-form-item label="创建时间" prop="createTime">
				<el-input v-model="form.createTime" clearable type="input"></el-input>
			</el-form-item>
			-->
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import setting from './setting'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					name: "",
					orderNum: 100,
					remark: ""
				},
				//验证规则
				rules: {
					orderNum: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					name: [
						{required: true, message: '请输入角色名称'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid == false) return;
					this.isSaveing = true;
					var apiObj;
					if (this.form.id && this.form.id > 0)
						apiObj = setting.api.edit;
					else
						apiObj = setting.api.add;
					var res = await apiObj.post(this.form);
					this.isSaveing = false;
					if(res.code === setting.successCode){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.name = data.name
				this.form.orderNum = data.orderNum
				this.form.remark = data.remark
			}
		}
	}
</script>

<style>
</style>
